package com.Service.Impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.Dao.BaseDaoI;
import com.Entity.MemberInfo;
import com.Service.MemberService;

@Service("memberservice")
public class MemberServiceImpl extends BaseServiceImpl<MemberInfo> implements
		MemberService {

	@Autowired
	private BaseDaoI<MemberInfo> memberDao;

	private List<MemberInfo> memberlist = new ArrayList<MemberInfo>();

	@Override
	public List<MemberInfo> findByUserid(int userid) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("userid", userid);

		String hql = "from MemberInfo t where t.userId = :userid";
		memberlist = this.memberDao.find(hql, params);
		if (null != memberlist && !memberlist.isEmpty()) {
			return memberlist;
		} else {
			return null;
		}
	}

	@Override
	public List<MemberInfo> findById(int familyid, int userid) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("familyid", familyid);
		params.put("userid", userid);
		String hql = "from MemberInfo t where t.familyId = :familyid and t.userId = :userid";
		memberlist = this.memberDao.find(hql, params);
		if (null != memberlist && !memberlist.isEmpty()) {
			return memberlist;
		} else {
			return null;
		}
	}

	@Override
	public List<MemberInfo> findByFamilyid(int familyid) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("familyid", familyid);
		String hql = "from MemberInfo t where t.familyId = :familyid";
		memberlist = this.memberDao.find(hql, params);
		if (null != memberlist && !memberlist.isEmpty()) {
			return memberlist;
		} else {
			return null;
		}
	}

	@Override
	public int deleteMember(int familyid) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("familyid", familyid);
		String hql = "from MemberInfo t where t.familyId = :familyid ";
		memberlist = this.memberDao.find(hql, params);
		if (null != memberlist && !memberlist.isEmpty()) {
			for (int i = 0; i < memberlist.size(); i++) {
				this.memberDao.delete(memberlist.get(i));
			}
			return 1;
		} else {
			return 0;
		}
	}

}
